﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Inspection
{
    public partial class 三分厂探伤统计 : System.Web.UI.Page
    {
        private string sql = @"SELECT 
       ts.[ID]
      ,ts.[日期]
      ,ts.[产品类别ID],
       CASE                                    
       WHEN ts.[产品类别ID]=3 THEN '油管接箍光管(J55)'                                                    
       WHEN ts.[产品类别ID]=4 THEN '套管接箍光管(J55)'
       WHEN ts.[产品类别ID]=103 THEN '油管接箍光管(N80)'                                                    
       WHEN ts.[产品类别ID]=104 THEN '套管接箍光管(N80)'
       END AS 产品类型 
      ,
       CASE                                    
       WHEN ts.[班次]=1 THEN '甲班'                                                    
       WHEN ts.[班次]=2 THEN '乙班'
       WHEN ts.[班次]=3 THEN '丙班'                                                    
       WHEN ts.[班次]=4 THEN '丁班'
       END AS 班次    
      ,ts.[加工数]
      ,ts.[合格数]
      ,ts.[不合格数]
      ,ts.[不合格原因]
      ,gh.名称 AS 供货单位
  FROM dbo.[三分厂探伤统计] AS ts INNER JOIN
       dbo.原料供货单位 AS gh ON gh.ID = ts.[供货单位]  ";
        protected void Page_Load(object sender, EventArgs e)
        {

            this.SqlDataSource1.SelectCommand = sql + " order by [日期] desc,[班次] asc";
            if (!this.IsPostBack)
            {
                Button_Query_Click(null, null);
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        //通用分页例程1
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        //通用分页例程2
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        //通用分页例程3
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }


        protected void Button_Query_Click(object sender, EventArgs e)
        {
            sql += " where 0=0 ";
            if (this.ChanPin.Text != "0")
            {
                sql += " and [产品类别ID]=" + this.ChanPin.Text.Replace("'", "");
            }

            if (this.GongDuan.Text != "0")
            {
                sql += " and [班次]=" + this.GongDuan.Text.Replace("'", "");
            }          
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and [日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and [日期]<='" + Dt.ToShortDateString() + "'";
            }
            sql += " order by [日期] desc,[班次] asc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.GridView1.SelectedIndex = -1;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongDuan.SelectedIndex = 0;
            this.ChanPin.SelectedIndex = 0;           
            this.Date1.Text = "";
            this.Date2.Text = "";
        }
    }
}
